package com.qiall.boot.fileservice.impl;

import com.qiall.boot.fileservice.StreamProcessInServer;
import com.qiall.boot.fileservice.data.eitity.FileInfo;
import com.qiall.boot.fileservice.data.mapper.FileInfoMapper;
import com.qiall.boot.fileservice.data.properties.OssProperties;
import com.qiall.boot.fileservice.impl.fileuploadSuccessEvent.UploadSuccessEvent;
import com.qiall.common.exception.BusinessException;
import com.qiall.common.exception.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Component("ossFileService")
public class OssFileServiceImpl implements StreamProcessInServer {

    @Autowired
    private OssProperties ossProperties;
    @Autowired
    private OssService ossService;
    @Autowired
    private FileInfoMapper fileInfoMapper;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public String getDownLoadUrl(String fromId) {
        String objectName = getObjectNameFromId(fromId);
        String tmpUrl = ossService.getOss().getTmpUrl(ossProperties.getBucketName(), objectName);
        return tmpUrl;
    }

    @Override
    public void deleteFie(String fromId) {
        // 先删除oss 上的文件
        String objectName = getObjectNameFromId(fromId);
        ossService.getOss().deleteObject(ossProperties.getBucketName(), objectName);
    }

    @Override
    public void uploadFile(MultipartFile multipartFile, Integer fileId) {
        try {
            String fileName = multipartFile.getOriginalFilename();
            String fileFullPath = ossProperties.getTmpFileLocation() + fileName;
            File tmpFile = new File(fileFullPath);
            if (!tmpFile.exists()) {
                tmpFile.createNewFile();
            }
            multipartFile.transferTo(tmpFile);
            FileInputStream fileInputStream = new FileInputStream(tmpFile);
            String objectName = buildObjectName(fileName);
            ossService.getOss().upload(ossProperties.getBucketName(), fileInputStream, objectName);
            //上传成功后删除临时文件
            tmpFile.delete();
            String fromId = buildFromId(ossProperties.getBucketName(), objectName);
            //发布上传成功的event
            UploadSuccessEvent uploadSuccessEvent = new UploadSuccessEvent(this, fileId, fromId);
            applicationContext.publishEvent(uploadSuccessEvent);
        } catch (IOException e) {
            throw new BusinessException(ResultCode.SYSTEM_ERROR, "文件上传失败");
        }
    }


    @Override
    public String uploadFileInner(MultipartFile multipartFile, Integer fileId) {
        try {
            byte[] bytes = multipartFile.getBytes();
            String orangeName = multipartFile.getOriginalFilename();
            String objectName = buildObjectName(orangeName);
            ossService.getOss().upload(ossProperties.getBucketNameInner(), new ByteArrayInputStream(bytes), objectName);
            // 返回下载文件地址
            String url = ossService.getOss().getPublicUrl(ossProperties.getBucketNameInner(), objectName);
            FileInfo fileInfo=new FileInfo();
            fileInfo.setId(fileId);
            String fromIdInner=buildFromId(ossProperties.getBucketNameInner(),objectName);
            fileInfo.setFromId(fromIdInner);
            fileInfoMapper.update(fileInfo);
            return url;
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(ResultCode.SYSTEM_ERROR, "文件上传失败");
        }

    }

    private String buildObjectName(String fileName) {
        long time = System.currentTimeMillis();
        return time + fileName;
    }

    private String buildFromId(String bucketName, String objectName) {
        return bucketName + "_" + objectName;
    }

    private String getObjectNameFromId(String fromId) {
        String prefix = ossProperties.getBucketName() + "_";
        String objectName = fromId.replace(prefix, "");
        return objectName;
    }

}
